package com.hilaver.dzmis.web.servlet.procurement;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.procurement.Procurement;
import com.hilaver.dzmis.service.impl.ProcurementServiceImpl;

public class ProcurementServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	public static final int ACTION_EDIT_ITEM = 100;
	
	public static final int ACTION_GET_ITEM_ALL = 101;
	
	public static final int ACTION_DELETE_ITEM = 102;

	private ProcurementServiceImpl pService;
	
	private Procurement py;

	public ProcurementServlet() {
		super();
		this.simpleName = Procurement.class.getSimpleName();
		this.fullName = Procurement.class.getName();
		this.pService = new ProcurementServiceImpl();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.pService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));

		String rtn = "";

		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			super.fillObj(request);
			this.py = (Procurement)this.obj;
			this.pService.edit(this.py);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.pService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.pService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setId(request);
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = this.pService.getAllPagination(this.page,
					this.sort, this.dir, this.filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.pService.getAll(this.id);
			break;
		case ACTION_EDIT_ITEM:
			this.setId(request);
			super.fillIds(request);
			rtn = this.pService.editItem(this.id, this.ids);
			break;
		case ACTION_GET_ITEM_ALL:
			this.setId(request);
			rtn = this.pService.getItemAll(this.id);
			break;
		case ACTION_DELETE_ITEM:
			this.setId(request);
			rtn = this.pService.deleteItem(this.id);
			break;
		}

		return rtn;
	}
}
